<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
    #box1
    {
        width:100px;
        height: 100px;
        background-color: red;
        /*开启绝对定位*/
        position:absolute;
    }
    </style>

    <script type="text/javascript">
    window.onload=function()
    {
        //使div可以跟随鼠标移动
        //获取box1
        var box1=document.getElementById("box1");
        //锁定鼠标移动事件
        document.onmousemove=function(event)
        {
            //获取鼠标的坐标
            //鼠标坐标定位是相对于当前浏览器框的定位
            //clientX和clientY是相当于浏览器框的定位
            //pageX和pageY是相对于页面的定位
            //var left=event.clientX;
            //var top=event.clientY;
            var left=event.pageX;
            var top=event.pageY;
            //pageX和page这两个属性在IE8中不支持，所以如果需要兼容IE8，就不能使用


            //设置div的偏移量
            box1.style.left=left+"px";
            box1.style.top=top+"px";
        };

        //获取box2
        var box2=document.getElementById("box2");
        box2.onmousemove=function(event)
        {
            event.cancelBubble=true;
        };
    };
    </script>
</head>
<body style="height: 1000px;width: 2000px;">
    <div id="box2" style="width: 500px;height: 500px;background-color: #bfa;"></div>
    <div id="box1"></div>
</body>
</html>